package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.AppVariable;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public AjaxResult login(HttpServletRequest request, String username, String password) {
        // 2.查询数据库
        User user = userService.getUserByName(username);

        if (user != null && user.getCode().length() > 0) {   // 有效的用户
            // 两个密码是否相同
            if (password.equals(user.getU_password())) {
                // 登录成功
                // 将用户存储到 session
                HttpSession session = request.getSession();
                session.setAttribute(AppVariable.USER_SESSION_KEY, user);
                user.setU_password(""); // 返回前端之前，隐藏敏感（密码）信息
                return AjaxResult.success(user);
            }
        }
        return AjaxResult.success(0, null);
    }

    @RequestMapping("/getanyuser")
    public AjaxResult getAnyUser(HttpServletRequest request){
        User user = UserSessionUtils.getUser(request);
        if (user == null) {
            return AjaxResult.fail(-1, "非法请求");
        }
        // 2.查询数据库
        List<User> list = userService.getAnyUser();
        return AjaxResult.success(list);
    }

    //删除用户数据
    @RequestMapping("/myDel")
    public AjaxResult myDel(HttpServletRequest request,String code){
        if (code == null || Integer.valueOf(code) <= 0) {
            // 参数有误
            return AjaxResult.fail(-1, "参数异常");
        }
        User user = UserSessionUtils.getUser(request);
        if (user == null) {
            return AjaxResult.fail(-2, "用户未登录");
        }
        return AjaxResult.success(userService.myDel(code));
    }

    @RequestMapping("/add")
    public AjaxResult add(HttpServletRequest request,User user){
        User userinfo = UserSessionUtils.getUser(request);
        if (user == null || Integer.valueOf(userinfo.getCode()) <= 0) {
            // 无效的登录用户
            return AjaxResult.fail(-2, "无效的登录用户");

        }
        return AjaxResult.success(userService.add(user));
    }

    @RequestMapping("/update")
    public AjaxResult update(HttpServletRequest request, String u_loginName, String password1){
        User user = UserSessionUtils.getUser(request);
        if (user == null || Integer.valueOf(user.getCode()) <= 0) {
            // 无效的登录用户
            return AjaxResult.fail(-2, "无效的登录用户");

        }
        return AjaxResult.success(userService.update(u_loginName,password1));
    }


}
